<template>
  <section class="todoapp">
    <Header :arr="list"></Header>
    <Main :arr="showList" @del="delFn"></Main>
    <Footer :arr="list" @changeType="changeType"></Footer>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
import Header from "./components/TodoHeader.vue";
import Main from "./components/TodoMain.vue";
import Footer from "./components/TodoFooter.vue";
export default {
  components: {
    Header,
    Main,
    Footer,
  },
  data() {
    return {
      //
      type: "all",
      list: JSON.parse(localStorage.getItem("todossx")) || [
        {
          id: 100,
          name: "吃饭",
          isDone: true,
        },
        {
          id: 201,
          name: "睡觉",
          isDone: false,
        },
        {
          id: 103,
          name: "打豆豆",
          isDone: true,
        },
      ],
    };
  },
  methods: {
    // 删除功能
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    changeType(type) {
      this.type = type;
    },
  },
  computed: {
    // 计算对应状态显示的数据
    showList() {
      if (this.type === "active") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "complete") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  // 存储本地
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("todossx", JSON.stringify(newVal));
      },
    },
  },
};
</script>
